﻿using Base.Api.Service;
using Common.AspNetCore;
using Common.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Base.Api.Controllers
{
    [Route("api/ServiceRegister")]
    public class ServiceRegisterController : ApiController
    {
        private readonly ServiceRegisterService authDataService;

        public ServiceRegisterController(IServiceProvider provider, ServiceRegisterService auth)
             : base(provider)
        {
            this.authDataService = auth;
        }

        [HttpGet, Route("DeletePublishService")]
        public async Task<ApiActionResult<string>> DeletePublishService(string url, string ip, string service, int delay = 30)
        {
            return await SafeExecuteAsync<string>(async (param) =>
            {
                return await this.authDataService.DeletePublishServiceAsync(new Models.ServiceRegisterParamModel()
                {
                    ServiceCode = service,
                    ServiceUrl = url,
                    DelaySecond = delay,
                    Ip = ip,
                });
            });
        }

        /// <summary>
        /// 校验服务是否启动，返回1启动成功 0未启动成功
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ip"></param>
        /// <param name="service"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        [HttpGet, Route("CheckServiceHealth")]
        public async Task<ApiActionResult<int>> CheckServiceHealth(string url, string ip, string service, int delay = 30)
        {
            return await SafeExecuteAsync(async (param) =>
            {
                return await this.authDataService.CheckServiceHealthAsync(new Models.ServiceRegisterParamModel()
                {
                    ServiceCode = service,
                    ServiceUrl = url,
                    DelaySecond = delay,
                    Ip = ip,
                });
            });
        }
    }
}
